package com.kafka.valid;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import com.kafka.common.String.StringValues;
import com.kafka.core.BaseClass;
import com.kafka.enumtype.MedicalType;
import com.kafka.enumtype.ResponseType;
import com.kafka.util.CheckUtil;

public class MedicalValid extends BaseClass{
	/**
	 * 绑定腕表校验
	 * @param message
	 * @param params
	 * @param result
	 * @param query
	 * @param msgId
	 * @return
	 */
	public static JSONObject validUpload(JSONObject params,String msgId) {
		JSONObject result = new JSONObject();
		Map<Object, Object> query = new HashMap<>();
		// 绑定设备或解绑设备
		try {
			//**************************必填字段校验*****************************************
			if(params.getString("uuid").isEmpty()){
				return failMsg(result,StringValues.UUID_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
//			if(params.getString("cardno").isEmpty()){
//				return failMsg(result,StringValues.CARDNO_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
//			}
			
			if(params.getString("type").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_TYPE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("index").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_INDEX_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("datetime").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_DATETIME_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(params.getString("value").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_VALUE_CAN_NOT_BE_NULL,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			//**************************查询字段校验*****************************************
			query.clear();
			query.put("uuid", params.getString("uuid"));
			if (userDao.selectByCondition(query) == null) {
				return failMsg(result,StringValues.UUID_NOT_FOUND,ResponseType.UUID_EXIST.toString(),msgId);
			}
			
			//**************************数据格式校验*****************************************
			if(!validTpe(params.getInt("type")) && !params.getString("type").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_TYPE_IS_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("type")) && !params.getString("type").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_TYPE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("index")) && !params.getString("index").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_INDEX_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isNumber(params.getString("value")) && !params.getString("value").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_VALUE_MUST_BE_NUMBER,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
			
			if(!CheckUtil.isDateTime(params.getString("datetime")) && !params.getString("datetime").isEmpty()){
				return failMsg(result,StringValues.MEDICAL_DATETIME_FORMAT_ERROR,ResponseType.DATA_FORMAT_ERROR.toString(),msgId);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return failMsg(result,e.toString(),ResponseType.SYSTEM_ERROR.toString(),msgId);
		}
		return null;
	}
	
	//校验体检类型
	private static boolean validTpe(int type){
		boolean isPass = false;
        for (MedicalType mt : MedicalType.values()){    
            if(Integer.valueOf(mt.toString()) == type){
            	isPass = true;
            }
        }
        return isPass;
	}

}
